/*---------------------------------------------------------
  DTribes Menu
---------------------------------------------------------*/
local PANEL = {}

PANEL.Tribetables = {}
PANEL.Tribetables["The Stranded"] = "gms_tribe1"
PANEL.Tribetables["Scavengers"] = "gms_tribe2"
PANEL.Tribetables["The Dynamics"] = "gms_tribe3"
PANEL.Tribetables["The Gummies"] = "gms_tribe4"
PANEL.Tribetables["Anonymous"] = "gms_tribe5"
PANEL.Tribetables["Survivalists"] = "gms_tribe6"

function PANEL:Init()
    self.Label = vgui.Create( "DLabel", self )
	self.Label:SetText( "Tribes Menu" )
	self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
	self.Label:SizeToContents( )
	CategoryList = vgui.Create( "DPanelList", self)
	CategoryList:SetPos( 25,25 )
	CategoryList:SetSize(ScrW() / 2 - 62, ScrH() - 104)

	CategoryList:SetSpacing( 5 )
	CategoryList:EnableHorizontal( false )
	CategoryList:EnableVerticalScrollbar( true )
         
    local size = ScrH() / 30
    local line = 25
	
	local SortTable = table.SortByKeyz( self.Tribetables )
	for k,v in pairs( SortTable ) do
		for txt,cmd in pairs ( v ) do
			local button = vgui.Create("gms_CommandButton",CategoryList)
			button:SetSize(CategoryList:GetWide() / 2-10, size)
			button:SetPos(10, line)
			button:SetConCommand(cmd.."\n")
			button:SetText(txt)
				 
			line = line + button:GetTall() + 10
		end
	end
	
	local createtribe = vgui.Create("gms_CommandButton",CategoryList)
        createtribe:SetSize(CategoryList:GetWide() / 2-20, size*6)
        createtribe:SetPos(CategoryList:GetWide() / 2+10, 25)
        createtribe:SetConCommand("gms_tribemenu\n")
        createtribe:SetText("Create-A-Tribe")
		
end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	self.Label:SetPos( 2, 2 )
end

vgui.Register("stranded_TribesMenu",PANEL,"DPanel")

/*---------------------------------------------------------
  DPlant Menu
---------------------------------------------------------*/
local PANEL = {}

PANEL.Planttables = {}
PANEL.Planttables["Plant Melon"] = "gms_plantmelon"
PANEL.Planttables["Plant Banana"] = "gms_plantbanana"
PANEL.Planttables["Plant Orange"] = "gms_plantorange"
PANEL.Planttables["Plant Tree"] = "gms_planttree"
PANEL.Planttables["Plant Grain"] = "gms_plantgrain"
PANEL.Planttables["Plant BerryBush"] = "gms_plantbush"

function PANEL:Init()
    self.Label = vgui.Create( "DLabel", self )
	self.Label:SetText( "Planting Menu" )
	self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
	self.Label:SizeToContents( )
	CategoryList = vgui.Create( "DPanelList", self)
	CategoryList:SetPos( 25,25 )
	CategoryList:SetSize(ScrW() / 2 - 62, ScrH() - 104)

	CategoryList:SetSpacing( 5 )
	CategoryList:EnableHorizontal( false )
	CategoryList:EnableVerticalScrollbar( true )
         
    local size = ScrH() / 30
    local line = 25
	
	local SortTable = table.SortByKeyz( self.Planttables )
	for k,v in pairs( SortTable ) do
		for txt,cmd in pairs( v ) do
			local button = vgui.Create("gms_CommandButton",CategoryList)
			button:SetSize(CategoryList:GetWide() / 2, size)
			button:SetPos(10, line)
			button:SetConCommand(cmd.."\n")
			button:SetText(txt)
				 
			line = line + button:GetTall() + 10
		end
	end

end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	self.Label:SetPos( 2, 2 )
end

vgui.Register("stranded_PlantSpawn",PANEL,"DPanel")


-- PROP MENU --
local PANEL = {};

function PANEL:Init()
	local propTable = table.SortByKeyz(GMS_SpawnLists);
	
	self:SetPadding(2);
	self:SetSpacing(2);
	self:EnableVerticalScrollbar(true);

	for _, tbl in pairs(propTable) do
		for name, v in pairs (tbl) do
			local category = vgui.Create("DCollapsibleCategory");
			category:SetLabel(name);
			category:SetExpanded(0);
			
			local iconList = vgui.Create("DPanelList");
			iconList:SetAutoSize(true);
			iconList:SetSpacing(2);
			iconList:SetPadding(2);
			iconList:EnableHorizontal(true);
			
			for __, model in pairs(v) do
				local spawnIcon = vgui.Create("SpawnIcon");
				spawnIcon:SetModel(model);
				spawnIcon:SetIconSize(64);
				spawnIcon.DoClick = function()
					RunConsoleCommand("gm_spawn", model, 0);
				end;
				
				iconList:AddItem(spawnIcon);
			end;
			
			category:SetContents(iconList);
			self:AddItem(category);
		end;
	end;
end;

function PANEL:PerformLayout()
	self:StretchToParent(2, 24, 2, 2);
	
	DPanelList.PerformLayout(self);
end;
vgui.Register("stranded_PropSpawn", PANEL, "DPanelList");

-- Inventory Panel.
local PANEL = {};

function PANEL:Init()
	self:SetSpacing(4);
	self:SetPadding(2);

	self:RefreshResources();
end;

function PANEL:RefreshResources()
	self.IntResources = 0;
	local SortTable = table.SortByKeyz(GAMEMODE.inventory);
	
	self:Clear();
	
	self.wForm = vgui.Create("DForm");
	self.wForm:SetName("Weight");
	self.wForm:SetPadding(4);
	
	self.weight = vgui.Create("DPanel");
	self.weight.Paint = function(self)
		local w, h = self:GetSize();
		
		draw.RoundedBox(4, 0, 0, w, h, Color(154, 154, 154, 255));
		
		if (self.maxWidth and self.maxWidth > 0.01) then
			draw.RoundedBox(4, 1, 1, math.Clamp((w -2) *self.maxWidth, 0, (w -2)), h -2, Color(157, 125, 102, 255));
		end;
		
		if (self.text) then
			draw.DrawText(self.text, "Default", w /2, h /2 -7, Color(255, 255, 255, 255), 1);
		end;
	end;
	
	self.iForm = vgui.Create("DForm");
	self.iForm:SetName("Inventory");
	self.iForm:SetPadding(4);

	self.itemList = vgui.Create("DPanelList");
	self.itemList:SetAutoSize(true);
	self.itemList:SetSpacing(2);
	self.itemList:SetPadding(2);
	self.itemList:EnableVerticalScrollbar(true);

	for _, inventory in pairs(SortTable) do
		for item, amount in pairs(inventory) do
			if (amount > 0) then
				local info = GAMEMODE.GetItemInfo(item);
				
				local DPanel = vgui.Create("DPanel");
				DPanel:SetTall(32);
				
				local spawnIcon = vgui.Create("SpawnIcon", DPanel);
				spawnIcon:SetIconSize(32);
				spawnIcon:SetModel(info.model);
				
				local nameLabel = vgui.Create("DLabel", DPanel);
				nameLabel:SetText(info.name .. " x" .. amount);
				nameLabel:SetPos(36, 2);
				nameLabel:SizeToContents();
				
				local infoLabel = vgui.Create("DLabel", DPanel);
				infoLabel:SetText(info.description);
				infoLabel:SetPos(36, 17);
				infoLabel:SizeToContents();
				
				
				local dropSlider = vgui.Create("DNumberWang", DPanel);
				dropSlider:SetPos(560, 14);
				dropSlider:SetSize(60, 17);
				dropSlider:SetText("");
				dropSlider:SetMinMax(1, amount);
				dropSlider:SetValue(1);
				dropSlider:SetDecimals(0);
				dropSlider:SetToolTip("Choose amount to drop/discard.");
				
				local dropButton = vgui.Create("DButton", DPanel);
				dropButton:SetSize(60, 17);
				dropButton:SetPos(495, 14);
				dropButton:SetText("Drop Item");
				dropButton.DoClick = function()
					RunConsoleCommand("gms_DropResources", item, dropSlider:GetValue());
				end;
				
				local discardButton = vgui.Create("DButton", DPanel);
				discardButton:SetSize(70, 17);
				discardButton:SetPos(0, 14);
				discardButton:MoveLeftOf(dropButton);
				discardButton:SetText("Discard Item");
				discardButton.DoClick = function()
					RunConsoleCommand("gms_DiscardResources", item, dropSlider:GetValue());
				end;
				
				if (info.command) then
					local useButton = vgui.Create("DButton", DPanel);
					useButton:SetSize(57, 17);
					useButton:SetPos(0, 14);
					useButton:MoveLeftOf(discardButton);
					useButton:SetText("Use Item");
					useButton.DoClick = function()
						RunConsoleCommand(info.command);
					end;
				end;
				
				self.IntResources = self.IntResources +amount;
				self.itemList:AddItem(DPanel);
			end;
		end;
	end;
	
	self.weight.maxWidth = self.IntResources / GAMEMODE.inventory_Max;
	self.weight.text = self.IntResources .. "/" .. GAMEMODE.inventory_Max .. "kg";
	
	self.wForm:AddItem(self.weight);
	self.iForm:AddItem(self.itemList);
	self:AddItem(self.wForm);
	self:AddItem(self.iForm);
/*
	local DropAllButton = vgui.Create("gms_CommandButton", PanelList)
	DropAllButton:SetSize(Width - 50, 50)
	DropAllButton:SetPos(self.Tab, self.Line)
	DropAllButton:SetText("Drop All Resources")
	function DropAllButton.DoClick(button)		
		LocalPlayer():ConCommand("gms_dropall".."\n")
	end

		*/
end

function PANEL:PerformLayout()
	self:StretchToParent(2, 24, 2, 2)
	self.iForm:SetTall(self:GetTall());
	
	DPanelList.PerformLayout(self);
end
vgui.Register("stranded_Inventory", PANEL, "DPanelList")

/*---------------------------------------------------------
  DOptions Menu
---------------------------------------------------------*/
local PANEL = {}

PANEL.Optiontables = {}
PANEL.Optiontables["Save Character"] = "gms_savecharacter"
function checkAdmin(player)  if player:IsAdmin() then 
PANEL.Optiontables["Admin Menu"] = "gms_admin"
end
end

function PANEL:Init()
    self.Label = vgui.Create( "DLabel", self )
	self.Label:SetText( "Options" )
	self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
	self.Label:SizeToContents( )
	CategoryList = vgui.Create( "DPanelList", self)
	CategoryList:SetPos( 25,25 )
	CategoryList:SetSize(ScrW() / 2 - 62, ScrH() - 104)

	CategoryList:SetSpacing( 5 )
	CategoryList:EnableHorizontal( false )
	CategoryList:EnableVerticalScrollbar( true )
         
    local size = ScrH() / 30
    local line = 25

		checkAdmin(LocalPlayer())
	local SortTable = table.SortByKeyz( self.Optiontables )
	for k,v in pairs( SortTable ) do
		for txt,cmd in pairs( v ) do
			local button = vgui.Create("gms_CommandButton",CategoryList)
			button:SetSize(CategoryList:GetWide() / 2, size)
			button:SetPos(10, line)
			button:SetConCommand(cmd.."\n")
			button:SetText(txt)
				 
			line = line + button:GetTall() + 10
		end
	end

end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	self.Label:SetPos( 2, 2 )
end

vgui.Register("stranded_Options",PANEL,"DPanel")

/*---------------------------------------------------------
  DCommands Menu
---------------------------------------------------------*/
local PANEL = {}

PANEL.Commandtables = {}
PANEL.Commandtables["Sleep"] = "gms_sleep"
PANEL.Commandtables["WakeUp"] = "gms_wakeup"
PANEL.Commandtables["Drop Weapon"] = "gms_DropWeapon"
PANEL.Commandtables["Make Campfire"] = "gms_makefire"
PANEL.Commandtables["Drink WaterBottle"] = "gms_DrinkBottle"
PANEL.Commandtables["Take Medicine"] = "gms_TakeMedicine"
PANEL.Commandtables["Combinations"] = "gms_GenericCombi"
PANEL.Commandtables["Structures"] = "gms_BuildingsCombi"
PANEL.Commandtables["Salvage Prop"] = "gms_salvage"

function PANEL:Init()
    self.Label = vgui.Create( "DLabel", self )
	self.Label:SetText( "Commands" )
	self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
	self.Label:SizeToContents( )
	CategoryList = vgui.Create( "DPanelList", self)
	CategoryList:SetPos( 25,25 )
	CategoryList:SetSize(ScrW() / 2 - 62, ScrH() - 104)

	CategoryList:SetSpacing( 5 )
	CategoryList:EnableHorizontal( false )
	CategoryList:EnableVerticalScrollbar( true )
         
    local size = ScrH() / 30
    local line = 25
	
	local SortTable = table.SortByKeyz( self.Commandtables )
	for k,v in pairs( SortTable ) do
		for txt,cmd in pairs( v ) do
			local button = vgui.Create("gms_CommandButton",CategoryList)
			button:SetSize(CategoryList:GetWide() / 2, size)
			button:SetPos(10, line)
			button:SetConCommand(cmd.."\n")
			button:SetText(txt)
				 
			line = line + button:GetTall() + 10
		end
	end

end

function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	self.Label:SetPos( 2, 2 )
end

vgui.Register("stranded_Commands",PANEL,"DPanel")

-- Tool menu.
local PANEL = {}

function PANEL:Init()
	self.Tools = vgui.Create("DPanelList", self);
	self.Tools:SetSpacing(2);
	self.Tools:SetPadding(2);
	self.Tools:EnableVerticalScrollbar(true);

	self.ContextPanel = vgui.Create("DPanelList", self);
	self.ContextPanel:EnableVerticalScrollbar(false);
	
	self.toolTable = spawnmenu.GetTools();

	if (self.toolTable) then
		for _, v in pairs(self.toolTable[1].Items) do 
			if (type(v) == "table") then 	 
				local name, label = v.ItemName, v.Text;
				
				v.Text = nil;
				v.ItemName = nil;
				
				self:AddCategory(name, label, v);
			end;
		end;
	end;
end;

function PANEL:AddCategory(name, label, toolItems)
	local bAlt = true;

	self.Category = vgui.Create("DCollapsibleCategory");
 	self.Category:SetLabel(label);
	
 	self.CategoryContent = vgui.Create("DPanelList")
 	self.CategoryContent:SetAutoSize(true);
 	self.CategoryContent:SetDrawBackground(false);

 	for k, v in pairs(toolItems) do 
		local prohibit = nil;
		
		for p, q in pairs (GMS.ProhibitedStools) do
			if (q == v.ItemName) then
				prohibit = true;
			end;
		end;
		
		if (prohibit != true) then
			local Item = vgui.Create("ToolMenuButton", self);
			Item:SetText(v.Text);
			Item.OnSelect = function(button) self:EnableControlPanel(button) end 
			
			concommand.Add(Format("tool_%s", v.ItemName), function() Item:OnSelect(); end);
		
			if (v.SwitchConVar) then 
				Item:AddCheckBox(v.SwitchConVar);
			end;

			Item.ControlPanelBuildFunction = v.CPanelFunction;
			Item.Command = v.Command;
			Item.Name = v.ItemName;
			Item.Controls = v.Controls;
			Item.Text = v.Text;

			Item:SetAlt(bAlt);
			bAlt = !bAlt;

			self.CategoryContent:AddItem(Item);
		end;
 	end;
	
	self.Category:SetContents(self.CategoryContent);
	self.Tools:AddItem(self.Category);
end;

function PANEL:EnableControlPanel(button) 
	if (self.LastSelected) then 
		self.LastSelected:SetSelected(false);
	end;

	button:SetSelected(true);
	self.LastSelected = button;

	local cp = controlpanel.Get(button.Name);
	
	if (!cp:GetInitialized()) then 
		cp:FillViaTable(button);
	end;

	self.ContextPanel:Clear();
	self.ContextPanel:AddItem(cp);
	self.ContextPanel:Rebuild();

	g_ActiveControlPanel = cp;

	if (button.Command) then 
		LocalPlayer():ConCommand(button.Command);
	end;
end;

function PANEL:Paint()
	local w, h = self:GetSize();
	
	draw.RoundedBox(4, 0, 0, w, h, Color(50, 50, 50, 255));
end;

function PANEL:PerformLayout()
	self:StretchToParent(2, 24, 2, 2);
	
	local w, h = self:GetSize();
	
	self.Tools:SetPos(2, 2);
	self.Tools:SetSize(300, h -4);
	
	self.ContextPanel:SetPos(w -334, 0);
	self.ContextPanel:SetSize(330, h -4);
end
vgui.Register("stranded_ToolMenu", PANEL, "DPanel");

/*---------------------------------------------------------
  DSPP Menu
---------------------------------------------------------*/
local PANEL = {}

PANEL.ACommands = {}
PANEL.ACommands["Prop Protection On/Off"] = "SPropProtection_toggle"
PANEL.ACommands["Admins Can Do Everything On/Off"] = "SPropProtection_admin"
PANEL.ACommands["Use Protection On/Off"] = "SPropProtection_use"
PANEL.ACommands["Entity Damage Protection On/Off"] = "SPropProtection_edmg"
PANEL.ACommands["Physgun Reload Protection On/Off"] = "SPropProtection_pgr"
PANEL.ACommands["Admins Can Touch World Props On/Off"] = "SPropProtection_awp"
PANEL.ACommands["Disconnect Prop Deletion On/Off"] = "SPropProtection_dpd"
PANEL.ACommands["Delete Admin Entities On/Off"] = "SPropProtection_dae"

PANEL.Sliders = {};
PANEL.Sliders["Deletion Delay in seconds"] = "SPropProtection_delay"

PANEL.AButtons = {};

PANEL.PlCBoxes = {};

PANEL.pnlCanvas = nil;

PANEL.LastThink = CurTime();

function PANEL:Init()
	CreateClientConVar("SPropProtection_toggle", 1, false, true)
	CreateClientConVar("SPropProtection_admin", 1, false, true)
	CreateClientConVar("SPropProtection_use", 1, false, true)
	CreateClientConVar("SPropProtection_edmg", 1, false, true)
	CreateClientConVar("SPropProtection_pgr", 1, false, true)
	CreateClientConVar("SPropProtection_awp", 1, false, true)
	CreateClientConVar("SPropProtection_dpd", 1, false, true)
	CreateClientConVar("SPropProtection_dae", 0, false, true)
	CreateClientConVar("SPropProtection_delay", 120, false, true)
    self.Label = vgui.Create( "DLabel", self )
	self.Label:SetText( "Prop Protection Menu" )
	self.Label:SetTextColor( Color( 255, 255, 255, 255 ) )
	self.Label:SizeToContents( )
	
	self.pnlBackground = vgui.Create( "DPanel", self)
	self.pnlBackground:SetPos( 25,25 )
	self.pnlBackground:SetSize( ScrW() / 2 - 62, ScrH() - 104)
	self.pnlBackground.Paint = function() 
		surface.SetDrawColor( 0, 0, 0, 0 )
		surface.DrawRect( 0, 0, self.pnlBackground:GetWide(), self.pnlBackground:GetTall() )
	end 

	self.pnlCanvas = vgui.Create( "DPanel", self.pnlBackground)
	self.pnlCanvas:SetPos( 0,0 )
	self.pnlCanvas:SetSize(ScrW() / 2 - 62, 2000)
	self.pnlCanvas.Paint = function() 
		surface.SetDrawColor( StrandedBorderTheme )
		surface.DrawRect( 0, 0, self.pnlCanvas:GetWide(), self.pnlCanvas:GetTall() ) 		
	end 

	self.VBar = vgui.Create("DVScrollBar", self);
	self.VBar:SetPos( (ScrW() / 2)  - 32, 25 ) 
	self.VBar:SetSize( 16,  ScrH() - 104 )
	self.VBar:SetUp( self.VBar:GetTall(), self.pnlCanvas:GetTall() )


	local line = 25
	local size = ScrH() / 35
	/*
	------------------------------------------------------------------------------------------------------Admin-----------------------------------------------------------------------------------------------------
	*/
	
	if(LocalPlayer():IsAdmin() or LocalPlayer():IsSuperAdmin())then
		local SortTable = table.SortByKeyz( self.ACommands )
		for k,v in pairs( SortTable ) do
			for txt,cmd in pairs( v ) do
				local cbox = vgui.Create("DCheckBoxLabel",self.pnlCanvas)
				cbox:SetSize(self.pnlCanvas:GetWide() / 2, 100)
				cbox:SetPos(self.pnlCanvas:GetWide() / 2, line)
				cbox:SetConVar(cmd)
				cbox:SetText(txt)
				cbox:SetValue(1);
					 
				line = line + 30 + 10
			end
		end
		
		local SortTable = table.SortByKeyz( self.Sliders )
		for k,v in pairs( SortTable ) do
			for txt,cmd in pairs( v ) do
				local slider = vgui.Create("DNumSlider",self.pnlCanvas)
				slider:SetSize(200, 100)
				slider:SetPos(self.pnlCanvas:GetWide() / 2, line)
				slider:SetConVar(cmd)
				slider:SetText(txt)
				slider:SetValue(600);
				slider:SetMin(0);
				slider:SetMax(1200);
				slider:SetDecimals(0);
					 
				line = line + 50 + 10
			end
		end

		AplAdmin = vgui.Create("gms_CommandButton",self.pnlCanvas)
		AplAdmin:SetSize(self.pnlCanvas:GetWide() / 2.5, size)
		AplAdmin:SetPos(self.pnlCanvas:GetWide() / 2, line)
		AplAdmin:SetConCommand("SPropProtection_ApplayerAdminSettings\n")
		AplAdmin:SetText("Applayer Admin Settings");
		line = line + AplAdmin:GetTall() + 10
		
		RelAdmin = vgui.Create("gms_CommandButton",self.pnlCanvas)
		RelAdmin:SetSize(self.pnlCanvas:GetWide() / 4, size)
		RelAdmin:SetPos(self.pnlCanvas:GetWide() / 2, line)
		RelAdmin:SetConCommand("SPropProtection_AdminReload\n")
		RelAdmin:SetText("Reload Settings");
		line = line + RelAdmin:GetTall() + 10
		
		
		line = 25;
		local SortTable = table.SortByKeyz( player.GetAll() )
		for k,v in pairs( SortTable ) do
			for k, player in pairs( v ) do
				if(player and player:IsValid())then
					PANEL.AButtons[k] = vgui.Create("gms_CommandButton",self.pnlCanvas)
					PANEL.AButtons[k]:SetSize(self.pnlCanvas:GetWide() / 3, size)
					PANEL.AButtons[k]:SetPos(10, line)
					PANEL.AButtons[k]:SetConCommand("SPropProtection_CleanupProps " ..player:GetNWString("SPPSteamID") .."\n")
					PANEL.AButtons[k]:SetText("Cleanup " ..player:Nick())
					
					line = line + PANEL.AButtons[k]:GetTall() + 10
				end
			end
		end
		local a = table.getn(PANEL.AButtons)+1;
		PANEL.AButtons[a] = vgui.Create("gms_CommandButton",self.pnlCanvas)
		PANEL.AButtons[a]:SetSize(self.pnlCanvas:GetWide() / 3, size)
		PANEL.AButtons[a]:SetPos(10, line)
		PANEL.AButtons[a]:SetConCommand("SPropProtection_CleanupDisconnectedProps\n")
		PANEL.AButtons[a]:SetText("Cleanup Disconnected");
		line = line + PANEL.AButtons[a]:GetTall() + 10
		
		if(line<550)then
			line = 550;
		end
	end
	
	/*
	-----------------------------------------------------------------------------------------------------Client-----------------------------------------------------------------------------------------------------
	*/
	AplBuddy = vgui.Create("gms_CommandButton",self.pnlCanvas)
	AplBuddy:SetSize(self.pnlCanvas:GetWide() / 4, size)
	AplBuddy:SetPos(self.pnlCanvas:GetWide() / 2, 550)
	AplBuddy:SetConCommand("SPropProtection_ApplayerBuddySettings\n")
	AplBuddy:SetText("Applayer Settings");
	
	ClBuddy = vgui.Create("gms_CommandButton",self.pnlCanvas)
	ClBuddy:SetSize(self.pnlCanvas:GetWide() / 4, size)
	ClBuddy:SetPos(self.pnlCanvas:GetWide() / 2, 550 + AplBuddy:GetTall() + 10)
	ClBuddy:SetConCommand("SPropProtection_ClearBuddies\n")
	ClBuddy:SetText("Clear All Buddies");
	
	local Players = player.GetAll()

	for k, player in pairs(Players) do
			if(player and player:IsValid() and player != LocalPlayer()) then
				local BCommand = "SPropProtection_BuddyUp_"..player:GetNWString("SPPSteamID")
				if(!LocalPlayer():GetInfo(BCommand)) then
					CreateClientConVar(BCommand, 0, false, true)
				end
			PANEL.PlCBoxes[k] = vgui.Create("DCheckBoxLabel",self.pnlCanvas)
	        PANEL.PlCBoxes[k]:SetSize(self.pnlCanvas:GetWide() / 4, size)
	        PANEL.PlCBoxes[k]:SetPos(10, line)
	        PANEL.PlCBoxes[k]:SetConVar(BCommand)
	        PANEL.PlCBoxes[k]:SetText(player:Nick())
			PANEL.PlCBoxes[k]:SetValue(0);
			PANEL.PlCBoxes[k]:SizeToContents();
	             
	        line = line + 30 + 10
		end
	end
	

	

end

function PANEL:Think()
	if(CurTime() >= PANEL.LastThink+1)then
		PANEL.LastThink = CurTime()
		--print("SPP Menu Update");
		local line = 25
		local size = ScrH() / 30
		
	/*
	------------------------------------------------------------------------------------------------------Admin-----------------------------------------------------------------------------------------------------
	*/
		if(LocalPlayer():IsAdmin() or LocalPlayer():IsSuperAdmin())then
			for k, v in pairs(PANEL.AButtons) do
				PANEL.AButtons[k]:Remove()
				PANEL.AButtons[k] = nil;
			end
			
			for k, player in pairs(player.GetAll()) do
				if(player and player:IsValid())then
					PANEL.AButtons[k] = vgui.Create("gms_CommandButton",self.pnlCanvas)
			        PANEL.AButtons[k]:SetSize(self.pnlCanvas:GetWide() / 3, size)
			        PANEL.AButtons[k]:SetPos(10, line)
			        PANEL.AButtons[k]:SetConCommand("SPropProtection_CleanupProps " ..player:GetNWString("SPPSteamID") .."\n")
			        PANEL.AButtons[k]:SetText("Cleanup " ..player:Nick())
					
			        line = line + PANEL.AButtons[k]:GetTall() + 10
				end
			end
			
			local a = table.getn(PANEL.AButtons)+1;
			PANEL.AButtons[a] = vgui.Create("gms_CommandButton",self.pnlCanvas)
			PANEL.AButtons[a]:SetSize(self.pnlCanvas:GetWide() / 3, size)
			PANEL.AButtons[a]:SetPos(10, line)
			PANEL.AButtons[a]:SetConCommand("SPropProtection_CleanupDisconnectedProps\n")
			PANEL.AButtons[a]:SetText("Cleanup Disconnected");
			line = line + PANEL.AButtons[a]:GetTall() + 10
			
			if(line < 550)then
				line = 550;
			end
		end
	/*
	------------------------------------------------------------------------------------------------------Client-----------------------------------------------------------------------------------------------------
	*/
		for k, v in pairs(PANEL.PlCBoxes)do
			PANEL.PlCBoxes[k]:Remove()
			PANEL.PlCBoxes[k] = nil;
		end
		
		for k, player in pairs(player.GetAll()) do
			if(player and player:IsValid() and player != LocalPlayer()) then
				local BCommand = "SPropProtection_BuddyUp_"..player:GetNWString("SPPSteamID")
				if(!LocalPlayer():GetInfo(BCommand)) then
					CreateClientConVar(BCommand, 0, false, true)
				end
				PANEL.PlCBoxes[k] = vgui.Create("DCheckBoxLabel",self.pnlCanvas)
		        PANEL.PlCBoxes[k]:SetPos(10, line)
		        PANEL.PlCBoxes[k]:SetConVar(BCommand)
		        PANEL.PlCBoxes[k]:SetText(player:Nick())
				PANEL.PlCBoxes[k]:SetValue(0);
				PANEL.PlCBoxes[k]:SizeToContents();
		             
		        line = line + 30 + 10
			end
		end
		
	end
	
end


function PANEL:PerformLayout( )
	self:StretchToParent( 2, 24, 2, 2 )
	self.Label:SetPos( 2, 2 )
end

function PANEL:OnVScroll( iOffset ) 
	self.pnlCanvas:SetPos( 0, iOffset )
end 

function PANEL:OnMouseWheeled( dlta ) 
	if ( self.VBar ) then 
		return self.VBar:OnMouseWheeled( dlta ) 
	end
end 

vgui.Register("stranded_SPPMenu",PANEL,"DPanel")